package com.ljx.blog.user.typehandler;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljx.blog.article.domain.entity.Theme;
import com.ljx.blog.article.domain.vo.resp.ThemeResp;
import com.ljx.blog.article.typehandler.custom.TagList;
import com.ljx.blog.user.typehandler.custom.ThemeRespList;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-13  12:50
 */
@MappedTypes(ThemeRespList.class)
public class TenantTypeHandler extends BaseTypeHandler<ThemeRespList> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, ThemeRespList themes, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSONUtil.toJsonStr(themes));
    }

    @Override
    public ThemeRespList getNullableResult(ResultSet resultSet, String s) throws SQLException {
        List<ThemeResp> list = JSONUtil.toList(resultSet.getString(s), ThemeResp.class);
        return new ThemeRespList(list);
    }

    @Override
    public ThemeRespList getNullableResult(ResultSet resultSet, int i) throws SQLException {
        List<ThemeResp> list = JSONUtil.toList(resultSet.getString(i), ThemeResp.class);
        return new ThemeRespList(list);
    }

    @Override
    public ThemeRespList getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        List<ThemeResp> list = JSONUtil.toList(callableStatement.getString(i), ThemeResp.class);
        return new ThemeRespList(list);
    }
}